const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') 
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const computedDate = (minDate, currDate, maxDate, tag) => {
  let time1 = 0
  let time2 = 0
  let time3 = 0
  if (tag === 1) {
    time1 = maxDate - currDate // 当前时间与选中日期相差毫秒数
    // 相差天数
    time2 = time1 / 86400000 | 0
    // 相差的时分秒  
    time3 = time1 - time2 * 86400000
    return currDate + time3
  } else {
    time1 = tag === 0 ? currDate - minDate : maxDate - currDate
    time2 = time1 / 86400000 | 0
    // 相差的时分秒  
    time3 = time1 - time2 * 86400000
    return time3
  }
}

// 格式化榜单 截止时间 格式
const formatClosingTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const dateStr ='截止时间：' + year + '年' + month + '月' + day + '日' + hour + '时'

  return dateStr
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  computedDate,
  formatClosingTime
}
